package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.QualityProductDefect;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.QualityProductDefectDao;
import com.efast.dmom.mes.datahandlers.base.QualityBaseDataHandler;
import com.efast.dmom.mes.entity.QualityProductDefectEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;
@Component
public class QualityProductDefectHandler extends QualityBaseDataHandler<QualityProductDefect>{

	@Autowired
	private QualityProductDefectDao qualityProductDefectDao;
	
	@Override
	public QualityProductDefect getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public QualityProductDefect getByCode(String code) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void batchDeleteById(List<QualityProductDefect> list) {
		List<QualityProductDefectEntity> listEntity = new ArrayList<QualityProductDefectEntity>();
		list.forEach(item -> {
			listEntity.add(item.getInnerData());
		});
		qualityProductDefectDao.deleteBatch(listEntity);
	}

	@Override
	public ResultBean save(QualityProductDefect obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj.getInnerData() == null) {
			return setErrorMessage("Entity is null");
		}
		BaseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		int save = qualityProductDefectDao.save(obj.getInnerData());
		if (save == 0) {
			return setErrorMessage("save fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(QualityProductDefect obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj.getInnerData() == null) {
			return setErrorMessage("Entity is null");
		}
		BaseEntityDataUtil.updateBaseEntityData(obj.getInnerData());
		int update = qualityProductDefectDao.update(obj.getInnerData());
		if (update == 0) {
			return setErrorMessage("update fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(QualityProductDefect obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean saveBatch(List<QualityProductDefect> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<QualityProductDefect> obj) {
		
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<QualityProductDefect> obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj.size() == 0) {
			return setErrorMessage("select is null");
		}
		List<QualityProductDefectEntity> list = new ArrayList<QualityProductDefectEntity>();
		obj.forEach(item -> {
			list.add(item.getInnerData());
		});
		int deleteBatch = qualityProductDefectDao.deleteBatch(list);
		if (deleteBatch == 0) {
			return setErrorMessage("delete fail");
		}
		return resultBean;
	}

	@Override
	public QualityProductDefect getById(long id) {
		QualityProductDefect qualityProductDefect = new QualityProductDefect();
		QualityProductDefectEntity byId = qualityProductDefectDao.getById(id);
		qualityProductDefect.setInnerData(byId);
		return qualityProductDefect;
	}

}
